/*
Customer
*/
#define APPLINE 1
#define PICLINE 2
#define PASLINE 3
#define CASHIER 4
#define CUSTOMER 5
#include "syscall.h"

int myexp ( int count ) {
  int i, val=1;
  for (i=0; i<count; i++ ) {
    val = val * 10;
  }
  return val;
}

void itoa( char arr[], int size, int val ) {
  int i, max, dig, subval, loc;
  for (i=0; i<size; i++ ) {
    arr[i] = '\0';
  }

  for ( i=1; i<=2; i++ ) {
    if (( val / myexp(i) ) == 0 ) {
      max = i-1;
      break;
    }
  }

  subval = 0;
  loc = 0;
  for ( i=max; i>=0; i-- ) {
    dig = 48 + ((val-subval) / myexp(i));
    subval = (dig-48) * myexp(max);
    arr[loc] = dig;
    loc++;
  }

  return;
}

void concatenate(char buf[], char arr1[], int len1, char arr2[], int len2){
	
	int i;

	for(i = 0; i<len1+len2; i++){
		if(i<len1){
			buf[i] = arr1[i];
		}
	  
		else if(i - len1 < len2){
			buf[i] = arr2[i - len1];
		}
		else buf[i] = '!';
	}
	
}

void Customer()
{
	char buf[256];
	char name[16];
	int index = 0;
	int x = 0;

  index = RegisterMe(CUSTOMER);
  itoa(&name[10],5,index);
  
  name[0] = 'C'; name[1] = 'U'; name[2] = 'S'; name[3] = 'T'; 
  name[4] = 'O'; name[5] = 'M'; name[6] = 'E'; name[7] = 'R';
  name[8] = ' '; name[9] = '#'; name[15] = '\0';

  concatenate(buf, name, 16, ": Customer Process created and currently running\n", 49);
  Write(buf,16+49,ConsoleOutput);
  concatenate(buf,name,16,": Entered passport office, standing in Application Line\n",56);
  Write(buf,16+56,ConsoleOutput);
  concatenate(buf,name,16,": Waiting for Application Clerk\n", 32);
  Write(buf,16+32,ConsoleOutput);
 

  PlaceMeInLine(index, APPLINE);


  concatenate(buf,name,16,": Finished interacting with Applicaton Clerk, standing in Picture Line\n", 71);
  Write(buf,16+71,ConsoleOutput);
  concatenate(buf,name,16,": Waiting for Picture Clerk\n", 28);
  Write(buf,16+28,ConsoleOutput);


  PlaceMeInLine(index, PICLINE);


  concatenate(buf,name,16,": Finished interacting with Picture Clerk, standing in Passport Line\n", 69);
  Write(buf,16+69,ConsoleOutput);
  concatenate(buf,name,16,": Waiting for Passport Clerk\n", 29);
  Write(buf,16+29,ConsoleOutput);


  PlaceMeInLine(index, PASLINE);


  concatenate(buf,name,16,": Finished interacting with Passport Clerk, standing in Cashier Line\n", 69);
  Write(buf,16+69,ConsoleOutput);
  concatenate(buf,name,16,": Waiting for Cashier Clerk\n", 28);
  Write(buf,16+28,ConsoleOutput);


  PlaceMeInLine(index, CASHIER);


  concatenate(buf,name,16,": Finished interacting with Cashier Clerk, RECEIVED PASSPORT - DONE WITH PASSPORT OFFICE!\n", 90);
  Write(buf,16+90,ConsoleOutput);



}





void main(){

  /*
    Run Customer  */

  Customer();


}


/*   Write(name, 16, ConsoleOutput); */
/*   Write(": Customer Process created and currently running\n",49, ConsoleOutput); */
/*   Write(name,16,ConsoleOutput); */
/*   Write(": Entered passport office, standing in Application Line\n",56, ConsoleOutput); */
/*   Write(name,16,ConsoleOutput); */
/*   Write(": Waiting for Application Clerk\n", 32, ConsoleOutput); */

 



/*   Write(name,16,ConsoleOutput); */
/*   Write(": Finished interacting with Applicaton Clerk, standing in Picture Line\n", 71, ConsoleOutput); */
/*   Write(name,16,ConsoleOutput); */
/*   Write(": Waiting for Picture Clerk\n", 28, ConsoleOutput); */



/*   Write(name,16,ConsoleOutput); */
/*   Write(": Finished interacting with Picture Clerk, standing in Passport Line\n", 69, ConsoleOutput); */
/*   Write(name,16,ConsoleOutput); */
/*   Write(": Waiting for Passport Clerk\n", 29, ConsoleOutput); */


/*   Write(name,16,ConsoleOutput); */
/*   Write(": Finished interacting with Cashier Clerk, RECEIVED PASSPORT - DONE WITH PASSPORT OFFICE!\n", 90, ConsoleOutput); */
/*                1         2         3         4         5         6         7         8         9         10*/
/*       1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 */
